package org.rcy.ruicingmarket.activity;

import org.rcy.ruicingmarket.common.result.PageResult;
import org.rcy.ruicingmarket.common.utils.CollectionBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

/**
*
* @author RuicyWu
*@Date 2022-02-08 15:22:09
*/
@Service
public class ActivityService{

	@Autowired
	private ActivityDao activityDao;

   /**
    * 分页查询
    * @param condition
    * @return
    */
	public PageResult<ActivityShowVo> queryPage(ActivityQueryCondition condition){
       int count = activityDao.queryPageCount(condition);
       List<Activity> activities = activityDao.queryPage(condition);
       return new PageResult<ActivityShowVo>(condition.getPageNo(),
               condition.getPageSize(),count, CollectionBeanUtils.copyListProperties(activities,ActivityShowVo::new));
    }

/**
 * 根据id查询ActivityShowVo
 * @param id
 * @return ActivityShowVo
 */
   public ActivityShowVo findById(Long id){
       Activity activity = activityDao.get(id);
      ActivityShowVo activityVo = new ActivityShowVo();
      BeanUtils.copyProperties(activity,activityVo);
      return activityVo;
   }

/**
 * 根据id删除Activity
 * @param id
 */
   @Transactional(rollbackFor = Exception.class)
   public void remove(Long id){activityDao.delete(id);}

/**
 * 新增Activity
 * @param vo
 */
   @Transactional(rollbackFor = Exception.class)
   public void add(ActivityEditVo vo){
      Activity activity = new Activity();
      BeanUtils.copyProperties(vo,activity);
      activityDao.insertSelective(activity);
   }

/**
 * 修改Activity
 * @param vo
 */
   @Transactional(rollbackFor = Exception.class)
   public void edit(ActivityEditVo vo){
      Activity activity = new Activity();
      BeanUtils.copyProperties(vo,activity);
      activityDao.update(activity,true);
   }

/**
 * 查询所有Activity
 * @return List<ActivityShowVo>
 */
   public List<ActivityShowVo> findAll(){
      return CollectionBeanUtils.copyListProperties(activityDao.getAllUnexpired(),ActivityShowVo::new);
   }
}
